#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
using namespace std;

const int N = 26;
int n, m;
vector<int> graph[N];
int indegree[N], temp_indegree[N];

// 拓扑排序，返回状态和序列
pair<int, string> topologicalSort() {
    memcpy(temp_indegree, indegree, sizeof(indegree));
    queue<int> q;
    
    // 找到所有入度为0的节点
    for (int i = 0; i < n; i++) {
        if (temp_indegree[i] == 0) {
            q.push(i);
        }
    }
    
    string result = "";
    bool multiple = false; // 是否有多个选择
    
    while (!q.empty()) {
        if (q.size() > 1) {
            multiple = true; // 有多个入度为0的节点，顺序不唯一
        }
        
        int u = q.front();
        q.pop();
        result += char(u + 'A');
        
        for (int v : graph[u]) {
            temp_indegree[v]--;
            if (temp_indegree[v] == 0) {
                q.push(v);
            }
        }
    }
    
    // 检查是否有环
    if (result.length() < n) {
        return {1, ""}; // 有环，矛盾
    }
    
    // 检查是否能确定唯一顺序
    if (multiple) {
        return {2, result}; // 顺序不唯一
    }
    
    return {0, result}; // 可以确定唯一顺序
}

int main() {
    cin >> n >> m;
    
    for (int i = 1; i <= m; i++) {
        string relation;
        cin >> relation;
        
        int u = relation[0] - 'A';
        int v = relation[2] - 'A';
        
        // 添加边
        graph[u].push_back(v);
        indegree[v]++;
        
        // 检查当前状态
        auto [status, sequence] = topologicalSort();
        
        if (status == 1) { // 发现矛盾
            cout << "Inconsistency found after " << i << " relations." << endl;
            return 0;
        } else if (status == 0) { // 确定顺序
            cout << "Sorted sequence determined after " << i << " relations: " << sequence << "." << endl;
            return 0;
        }
    }
    
    // 处理完所有关系后仍无法确定顺序
    cout << "Sorted sequence cannot be determined." << endl;
    
    return 0;
}